package com.lijin.study.java.merstat;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.lijin.study.java.util.FileLineProcessor;
import com.lijin.study.java.util.FileUtil;

public class MainStat {
	
	private static final String FILE_ROOTER = "D:\\工作目录\\02.日常工作\\生产数据处理\\商户统计\\";
	private static final String RESULT_ROOTER = "D:\\工作目录\\02.日常工作\\生产数据处理\\商户统计\\result\\";
	private static final String CHAR_SET = "UTF-8";
	
	public static void main(String[] args) throws Exception {

		printTotalMers();
		
//		Map<String, String> upopMers = readUpopMers();
//		Map<String, String> ebppMers = getEbppMers();
		
//		Set<String> ebppSet = new HashSet<String>();
//		Set<String> upopSet = new HashSet<String>();
//		Set<String> commonSet = new HashSet<String>();
//		for (String mcd : ebppMers.keySet()) {
//			if (upopMers.containsKey(mcd)) {
//				commonSet.add(mcd + "|" + ebppMers.get(mcd) + "|" + upopMers.get(mcd));
//			} else {
//				ebppSet.add(mcd + "|" + ebppMers.get(mcd) + "|");
//			}
//		}
//		for (String mcd : upopMers.keySet()) {
//			if (ebppMers.containsKey(mcd)) {
//				commonSet.add(mcd + "|" + ebppMers.get(mcd) + "|" + upopMers.get(mcd));
//			} else {
//				upopSet.add(mcd + "|" + "|" + upopMers.get(mcd));
//			}
//		}
		
//		List<String> contents = new ArrayList<String>();
//		contents.addAll(new ArrayList<String>(ebppSet));
//		contents.addAll(new ArrayList<String>(commonSet));
//		contents.addAll(new ArrayList<String>(upopSet));
		
//		File f = new File(RESULT_ROOTER + "upop_ebpp_mers.txt");
//		FileUtil.writeFileByLine(f, new ArrayList<String>(contents), "UTF-8");
		
//		Map<String, Map<String, Long>> upopHuoyueMers = compUpopMersTrans(upopMers);
//		printUpopHuoYueMers(upopHuoyueMers);
//		printUpopBuHuoYueMers(upopMers);
//		printRegUpopMer(upopMers);

//		Map<String, Map<String, Long>> upopHuoyueMers = compUpopMersTrans(upopMers);
//		Map<String, String> ebppMers = getEbppMers();
//		printEbppBuHuoyueMers(ebppMers);
//		Map<String, Map<String, Long>> ebppHuoyueMers = comptEbppHuoYueMers();
//		printEbppHuoYueMers(ebppMers, ebppHuoyueMers);
	}
	
	public static void printTotalMers() throws Exception {
		final Set<String> ebppmers = new HashSet<String>();
		final Set<String> upopmers = new HashSet<String>();
		final Set<String> commonmers = new HashSet<String>();
		
		File ebppF = new File(RESULT_ROOTER + "EBPP近3个月活跃商户交易量.txt");
		FileUtil.readFileByLine(ebppF, new FileLineProcessor() {
			@Override
			public void processLine(String line) {
				String[] mInf = line.split("[|]");
				if (mInf.length != 4) {
					System.out.println(line);
				} else {
					String qtat = mInf[2];
					if (Long.valueOf(qtat) == 0) {
						ebppmers.add(mInf[0]);
					}
				}
			}
		}, "UTF-8", 1);
		
		File upopF = new File(RESULT_ROOTER + "upop近3个月活跃商户交易量.txt");
		FileUtil.readFileByLine(upopF, new FileLineProcessor() {
			@Override
			public void processLine(String line) {
				String[] mInf = line.split("[|]");
				if (mInf.length != 4) {
					System.out.println(line);
				} else {
					String qtat = mInf[2];
					if (Long.valueOf(qtat) == 0) {
						if (ebppmers.contains(mInf[0])) {
							commonmers.add(mInf[0]);
						}
						upopmers.add(mInf[0]);
					}
				}
			}
		}, "UTF-8", 1);
		
		System.out.println(commonmers.size());
		System.out.println(ebppmers.size() + upopmers.size() - commonmers.size());
	}

	/**
	 * 统计注册商户情况
	 * @param upopMers
	 */
	public static void printRegUpopMer(final Map<String, String> upopMers) throws Exception {
		File f = new File(RESULT_ROOTER + "upop近3个月活跃商户交易量.txt");
		final List<String> contents = new ArrayList<String>();
		FileUtil.readFileByLine(f, new FileLineProcessor() {
			@Override
			public void processLine(String line) {
				String[] mInf = line.split("[|]");
				if (mInf.length != 4) {
					System.out.println(line);
				} else {
					if (upopMers.containsKey(mInf[0])) {
						String qtat = mInf[2];
						if (Long.valueOf(qtat) == 0) {
							contents.add(line);
						}
					}
				}
			}
		}, "UTF-8", 1);
		File outF = new File(RESULT_ROOTER + "upop_reg_buhuoyue.txt");
		FileUtil.writeFileByLine(outF, contents, "UTF-8");
	}
	
	/**
	 * 查找UPOP不活跃商户
	 */
	public static void printUpopBuHuoYueMers(final Map<String, String> upopMers) throws Exception {
		File f = new File(RESULT_ROOTER + "upop近3个月活跃商户交易量.txt");
		final Set<String> huoyueMers = new HashSet<String>();
		FileUtil.readFileByLine(f, new FileLineProcessor() {
			@Override
			public void processLine(String line) {
				String[] mInf = line.split("[|]");
				if (mInf.length != 4) {
					System.out.println(line);
				} else {
					huoyueMers.add(mInf[0].trim());
				}
			}
		}, "UTF-8", 1);
		
		f = new File(FILE_ROOTER + "UPOP_近3个月不活跃商户.log");
		final Map<String, String> buHuoyueMers = new HashMap<String, String>();
		FileUtil.readFileByLine(f, new FileLineProcessor() {
			@Override
			public void processLine(String line) {
				String[] mInf = line.split("[|]");
				if (mInf.length != 3) {
					System.out.println(line);
				} else {
					String mcd = mInf[0].trim();
					String mnm = mInf[1].trim();
					if (upopMers.containsKey(mcd)){
						buHuoyueMers.put(mcd, upopMers.get(mcd));
					} else {
						buHuoyueMers.put(mcd, mnm);
					}
				}
			}
		}, "GBK", 1);
		
		Set<String> mers = new HashSet<String>();
		for (String mcd : buHuoyueMers.keySet()) {
			mers.add(mcd + "|" + buHuoyueMers.get(mcd));
		}
		for (String mcd : upopMers.keySet()) {
			if (!huoyueMers.contains(mcd)) {
				mers.add(mcd + "|" + upopMers.get(mcd));
			}
		}
		
		File outF = new File(RESULT_ROOTER + "upop_buhuoyue_mers.txt");
		List<String> contents = new ArrayList<String>(mers);
		FileUtil.writeFileByLine(outF, contents, CHAR_SET);
	}
	
	/**
	 * 打印出Upop活跃商户交易量
	 * @param upopHuoyueMers
	 * @throws Exception
	 */
	public static void printUpopHuoYueMers(Map<String, Map<String, Long>> upopHuoyueMers) throws Exception {
		File f = new File(RESULT_ROOTER + "upop_huoyue_mers.txt");
		List<String> contents = new ArrayList<String>();
		for (String mcd : upopHuoyueMers.keySet()) {
			StringBuilder b = new StringBuilder();
			b.append(mcd).append("|")
			 .append(upopHuoyueMers.get(mcd).get("QT") == null ? 0 : upopHuoyueMers.get(mcd).get("QT")).append("|")
			 .append(upopHuoyueMers.get(mcd).get("HT") == null ? 0 : upopHuoyueMers.get(mcd).get("HT"));
			contents.add(b.toString());
		}
		FileUtil.writeFileByLine(f, contents, CHAR_SET);
	}
	
	/**
	 * 统计UPOP前后台交易
	 * @return
	 */
	public static Map<String, Map<String, Long>> compUpopMersTrans(final Map<String, String> upopMers) throws Exception {
		
		final Map<String, Map<String, Long>> result = new HashMap<String, Map<String, Long>>();
		final Map<String, Long> s1 = new HashMap<String, Long>();
		final Map<String, Long> s2 = new HashMap<String, Long>(); 
		
		File f = new File(FILE_ROOTER + "UPOP_3个月内前台商户交易.log");
		FileUtil.readFileByLine(f, new FileLineProcessor() {
			@Override
			public void processLine(String line) {
				String[] mInf = line.split("[|]");
				if (mInf.length != 3) {
					System.out.println(line);
				} else {
					String key = null;
					String mcd = mInf[0].trim();
					String mnm = mInf[1].trim();
					String amt = mInf[2].trim();
					Long at = Long.valueOf(amt.substring(0, amt.indexOf(".")));
					if (upopMers.containsKey(mcd)) {
						key = mcd + "|" + upopMers.get(mcd);
					} else {
						key = mcd + "|" + mnm;
					}
					if (!s1.containsKey(key)) {
						s1.put(key, at);
					} else {
						s1.put(key, s1.get(key) + at);
					}
				}
			}
		}, "GBK", 1);
		
		f = new File(FILE_ROOTER + "UPOP_3个月内后台商户交易.log");
		FileUtil.readFileByLine(f, new FileLineProcessor() {
			@Override
			public void processLine(String line) {
				String[] mInf = line.split("[|]");
				if (mInf.length != 3) {
					System.out.println(line);
				} else {
					String key = null;
					String mcd = mInf[0].trim();
					String mnm = mInf[1].trim();
					String amt = mInf[2].trim();
					Long at = Long.valueOf(amt.substring(0, amt.indexOf(".")));
					if (upopMers.containsKey(mcd)) {
						key = mcd + "|" + upopMers.get(mcd);
					} else {
						key = mcd + "|" + mnm;
					}
					if (!s2.containsKey(key)) {
						s2.put(key, at);
					} else {
						s2.put(key, s2.get(key) + at);
					}
				}
			}
		}, "GBK", 1);
		
		for (String key : s1.keySet()) {
			Map<String, Long> mi = new HashMap<String, Long>();
			mi.put("QT", s1.get(key));
			result.put(key, mi);
		}
		
		for (String key : s2.keySet()) {
			Map<String, Long> mi = null;
			if (result.containsKey(key)) {
				result.get(key).put("HT", s2.get(key));
			} else {
				mi = new HashMap<String, Long>();
				mi.put("HT", s2.get(key));
				result.put(key, mi);
			}
		}
		
		return result;
	}
	
	public static Map<String, String> readUpopMers() throws Exception {
		File f = new File(FILE_ROOTER + "UPOP商户.log");
		final Map<String, String> rst = new HashMap<String, String>();
		FileUtil.readFileByLine(f, new FileLineProcessor() {
			@Override
			public void processLine(String line) {
				String[] mInf = line.split("[|]");
				if (mInf.length != 3) {
					System.out.println(line);
				} else {
					rst.put(mInf[0].trim(), mInf[1].trim());
				}
			}
		}, "GBK", 1);
		return rst;
	}

	
	/**
	 * 找出EBPP不活跃的商户
	 * @param ebppMers
	 */
	public static void printEbppBuHuoyueMers(Map<String, String> ebppMers) throws Exception {
		File inF = new File(RESULT_ROOTER + "EBPP近3个月活跃商户交易量.txt");
		final Set<String> mers = new HashSet<String>();
		FileUtil.readFileByLine(inF, new FileLineProcessor() {
			@Override
			public void processLine(String line) {
				String[] merInfo = line.split("[|]");
				if (merInfo.length != 4) {
					System.out.println(line);
				} else {
					mers.add(merInfo[0].trim());
				}
			}
		}, CHAR_SET, 1);
		
		List<String> contents = new ArrayList<String>();
		for (String mcd : ebppMers.keySet()) {
			if (!mers.contains(mcd)) {
				contents.add(mcd + "|" + ebppMers.get(mcd));
			}
		}
		System.out.println(contents.size());
		File outF = new File(RESULT_ROOTER + "ebpp_huoyue_mers.txt");
		FileUtil.writeFileByLine(outF, contents, CHAR_SET);
	}
	
	/**
	 * 生成EBPP活跃的商户交易信息汇总结果
	 * @param ebppMers
	 * @param ebppHuoyueMers
	 * @throws Exception
	 */
	public static void printEbppHuoYueMers(Map<String, String> ebppMers, Map<String, Map<String, Long>> ebppHuoyueMers) throws Exception {
		File f = new File(RESULT_ROOTER + "ebpp_huoyue_mers.txt");
		List<String> contents = new ArrayList<String>();
		for (String mcd : ebppHuoyueMers.keySet()) {
			StringBuilder b = new StringBuilder();
			b.append(mcd).append("|")
			 .append(ebppMers.get(mcd)).append("|")
			 .append(ebppHuoyueMers.get(mcd).get("QT") == null ? 0 : ebppHuoyueMers.get(mcd).get("QT")).append("|")
			 .append(ebppHuoyueMers.get(mcd).get("HT") == null ? 0 : ebppHuoyueMers.get(mcd).get("HT"));
			contents.add(b.toString());
		}
		FileUtil.writeFileByLine(f, contents, CHAR_SET);
	}
	
	/**
	 * 找出EBPP活跃的商户
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Map<String, Long>> comptEbppHuoYueMers() throws Exception {
		File f = new File(FILE_ROOTER + "EBPP近3个月交易.txt");
		final Set<String> transCds = new HashSet<String>();
		transCds.add("1010");
		transCds.add("1013");
		transCds.add("1014");
		transCds.add("5021");
		transCds.add("5022");
		
		Map<String, Map<String, Long>> result = new HashMap<String, Map<String, Long>>();
		final Map<String, Long> s1 = new HashMap<String, Long>();
		final Map<String, Long> s2 = new HashMap<String, Long>(); 
		
		FileLineProcessor flp = new FileLineProcessor() {
			@Override
			public void processLine(String line) {
				String[] minfo = line.split("[|]");
				if (minfo.length != 3) {
					System.out.println(line);
				} else {
					String mcd = minfo[0].trim();
					String transCd = minfo[1].trim();
					String transAt = minfo[2].trim();
					Long at = Long.valueOf(transAt.substring(0, transAt.indexOf(".")));
					if (transCds.contains(transCd)) {
						if (s1.containsKey(mcd)) {
							s1.put(mcd, s1.get(mcd) + at);
						} else {
							s1.put(mcd, at);
						}
					} else {
						if (s2.containsKey(mcd)) {
							s2.put(mcd, s2.get(mcd) + at);
						} else {
							s2.put(mcd, at);
						}
					}
				}
			}
		};
		FileUtil.readFileByLine(f, flp, CHAR_SET, 1);
		
		for (String key : s1.keySet()) {
			Map<String, Long> mi = new HashMap<String, Long>();
			mi.put("QT", s1.get(key));
			result.put(key, mi);
		}
		
		for (String key : s2.keySet()) {
			Map<String, Long> mi = null;
			if (result.containsKey(key)) {
				result.get(key).put("HT", s2.get(key));
			} else {
				mi = new HashMap<String, Long>();
				mi.put("HT", s2.get(key));
				result.put(key, mi);
			}
		}
		
		return result;
	}
	
	public static Map<String, String> getEbppMers() throws Exception {
		File f = new File(FILE_ROOTER + "mchnt_info.txt");
		final Map<String, String> s = new HashMap<String, String>();
		FileLineProcessor flp = new FileLineProcessor() {
			@Override
			public void processLine(String line) {
				String[] minfo = line.split("[|]");
				if (minfo.length != 3) {
					System.out.println(line);
				} else {
					if (s.containsKey(minfo[0].trim())) {
						System.out.println(line);
					} else {
						s.put(minfo[0].trim(), minfo[1].trim());
					}
				}
			}
		};
		FileUtil.readFileByLine(f, flp, CHAR_SET, 1);
		return s;
	}
}
